package glemipsc;

import java.util.List;

import glemipsc.implementations.Argument;
import glemipsc.implementations.Implementation;
import glemipsc.implementations.Instruction;

public class TypeChar extends Type {
	public TypeChar() {
		typeFormat = Type.TypeFormat.CHAR;
	}
	
	public String getName() {
		return "char";
	}
	
	public boolean isCompatibleWith(Type type) {
		if (type.typeFormat != this.typeFormat && type.typeFormat != Type.TypeFormat.INTEGER)
			return false;
		return true;
	}

	public int getValueLength() {
		return GLEMipsC.compiler.environment.implementation.getCharSize()/8;
	}
	
	public int sizeOf() {
		return getValueLength();
	}
	
	public String toString() {
		return (this.staticElement ? "static " : "") + (this.signed ? "signed " : "unsigned ") + "char";
	}

	public boolean computeInitialization(Address el, Object value, List<Instruction> instructions) {
		Implementation impl = GLEMipsC.compiler.getEnvironment().implementation;
		
		if (value instanceof ConstantChar) {
			ConstantChar val = (ConstantChar) value;
			Argument result = impl.getFreeRegister();
			instructions.add(impl.getMov(result, impl.getImmediate(val.getImmediateValue())));
			instructions.add(impl.getSt(result, el, getValueLength()));
			impl.freeRegister(result);
		}
		else if (value instanceof ConstantNumber) {
			ConstantNumber val = (ConstantNumber) value;
			Argument result = impl.getFreeRegister();
			instructions.add(impl.getMov(result, impl.getImmediate(val.getImmediateValue())));
			instructions.add(impl.getSt(result, el, getValueLength()));
			impl.freeRegister(result);
		}
		else
			return false;
		
		return true;
	}
}
